﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using cs_dame.Model;
using cs_dame.View;
using cs_dame.Controler;

namespace cs_dame
{
    public partial class MainFrame : Form , IGUIView
    {
        private ISteuerung Controler;
        private PictureBox[,] PFeld;
        private bool isFirst = true;

        public SpielerTyp White { get; set; }
        public SpielerTyp Red { get; set; }
        public int Groesse { get; set;}

        public MainFrame()
        {
            InitializeComponent();
            Groesse = 8;
            White = SpielerTyp.MENSCH;
            Red = SpielerTyp.COMPUTER;
        }

        private void MainFrame_Load(object sender, EventArgs e)
        {
        }

        public void SetChange(Spielsteine[][] Spielfeld, bool isWhite, int[] SelectedField, String s)
        {
            Clear();
            InitialField(Spielfeld);
            label1.Text = s;
            if (SelectedField[0] != -1 && SelectedField[1] != -1)
                SelectField(SelectedField[0], SelectedField[1], Spielfeld);
        }

        private void pictureBox(object sender, EventArgs e)
        {
            PictureBox temp = (PictureBox)sender;
            int[] Koordinate = (int[]) temp.Tag;
            Controler.KlickAufFeld(Koordinate[0], Koordinate[1]);
        }

        private void InitialField(Spielsteine[][] feld)
        {

            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainFrame));
            PFeld = new PictureBox[feld.Length, feld.Length];
            System.Drawing.Size PSize = new System.Drawing.Size(50,50);
            
            for (int i = 0; i < feld.Length; ++i)
            {
                for (int j = 0; j < feld[i].Length; ++j)
                {
                    PictureBox temp = new PictureBox();
           //         System.Drawing.Image White = ((System.Drawing.Image)(resources.GetObject("$this.White")));
         //           System.Drawing.Image Black = ((System.Drawing.Image)(resources.GetObject("$this.Black")));
       //             System.Drawing.Image RedStone = ((System.Drawing.Image)(resources.GetObject("$this.RedStone")));
     //               System.Drawing.Image RedChecker = ((System.Drawing.Image)(resources.GetObject("$this.RedChecker")));
   //                 System.Drawing.Image WhiteStone = ((System.Drawing.Image)(resources.GetObject("$this.WhiteStone")));
 //                   System.Drawing.Image WhiteChecker = ((System.Drawing.Image)(resources.GetObject("$this.WhiteChecker")));

                    Bitmap White = new Bitmap("../../Data/White.png");
                    Bitmap Black = new Bitmap("../../Data/Black.png");
                    Bitmap RedStone = new Bitmap("../../Data/RedStone.png");
                    Bitmap RedChecker = new Bitmap("../../Data/RedChecker.png");
                    Bitmap WhiteStone = new Bitmap("../../Data/WhiteStone.png");
                    Bitmap WhiteChecker = new Bitmap("../../Data/WhiteChecker.png");
                    temp.Size = PSize;
                    temp.Name = "PictureBox" + i + j;
                    int[] Koordinate = { i, j };
                    temp.Tag = Koordinate;
                    temp.Location = new Point(50+PSize.Width * i,50+PSize.Height * j);
                    temp.Visible = true;
                    temp.Click += new System.EventHandler(pictureBox);
                    PFeld[i,j] = temp;
                    switch (feld[i][j]){
                        case Spielsteine.STEINSCHWARZ:
                            temp.Image = RedStone;
                            temp.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                            this.Controls.Add(temp);
                            break;
                        case Spielsteine.STEINWEISS:
                            temp.Image = WhiteStone;
                            temp.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                            this.Controls.Add(temp);
                            break;
                        case Spielsteine.DAMESCHWARZ:
                            temp.Image = RedChecker;
                            temp.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                            this.Controls.Add(temp);
                            break;
                        case Spielsteine.DAMEWEISS:
                            temp.Image = WhiteChecker;
                            temp.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                            this.Controls.Add(temp);
                            break;
                        case Spielsteine.FREI:
                            temp.Image = Black;
                            temp.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                            this.Controls.Add(temp);
                            break;
                        case Spielsteine.UNERREICHBAR:
                            temp.Image = White;
                            temp.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                            this.Controls.Add(temp);
                            break;
                        default: throw new ArgumentOutOfRangeException("'" + feld[i][j] + "' ist unbekannter Spielstein");
                    }
                }
            }
            isFirst = false;
        }

        private void SelectField(int x, int y, Spielsteine[][] Spielfeld)
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainFrame));
            switch (Spielfeld[x][y])
            {
                case Spielsteine.STEINSCHWARZ:
                    PFeld[x, y].Image = (new Bitmap("../../Data/RedStoneSelected.png"));
                    break;
                case Spielsteine.STEINWEISS:            
                    PFeld[x, y].Image = (new Bitmap("../../Data/WhiteStoneSelected.png"));
                    PFeld[x, y].Invalidate();
                    PFeld[x, y].Refresh();
                    PFeld[x, y].Update();
                    break;
                case Spielsteine.DAMESCHWARZ:
                    PFeld[x, y].Image = (new Bitmap("../../Data/RedCheckerSelected.png"));
                    break;
                case Spielsteine.DAMEWEISS:
                    PFeld[x, y].Image = (new Bitmap("../../Data/WhiteCheckerSelected.png"));
                    break;
                case Spielsteine.FREI:
                    PFeld[x, y].Image = (new Bitmap("../../Data/BlackSelected.png"));
                    break;
                case Spielsteine.UNERREICHBAR:
                    PFeld[x, y].Image = (new Bitmap("../../Data/WhiteSelected.png"));
                    break;
                default: throw new ArgumentOutOfRangeException("'" + Spielfeld[x][y] + "' ist unbekannter Spielstein"); 
            }
            PFeld[x, y].BackColor = Color.Blue;
        }

        private void Clear()
        {
            if (PFeld == null)
            {
                return;
            }
            for (int i = 0; i < Groesse; i++)
            {
                for (int j = 0; j < Groesse; j++)
                {
                    this.Controls.Remove(PFeld[i, j]);
                    PFeld[i, j] = null;
                }
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Controler = new Steuerung(White,Red,this,Groesse);
        }

        private void preferenceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form form = new View.Preference(this);
            form.Show();
        }
    }
}
